unit clsAtrib_Homem;

interface

uses dm,dbtables,biblio,sysutils;

type
  TAtrib_Homem=Class(TObject)
    public
      Descricao : string;
      Percentual: double;
      Operacao  : string;
      function  Recuperar(Cod_AtribH: string): boolean;
      function  Retorna_Ultimo               : string;
      procedure Gravar(Cod_AtribH: String);
      procedure Excluir(Cod_AtribH: String);
      procedure Consultar(p_Descricao: string; var tq: TQuery);
    end;

implementation

procedure TAtrib_Homem.Gravar;
var tq: TQuery;
begin
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Homem where Cod_AtribH=:a');
 tq.params[0].asstring:= Cod_AtribH;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Atrib_Homem values ('+
             ':k_Cod_AtribH,'+
             ':k_Descricao ,'+
             ':k_Percentual,'+
             ':k_Operacao  )');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Atrib_Homem set   '+
             'Descricao =:k_Descricao ,'+
             'Percentual=:k_Percentual,'+
             'Operacao  =:k_Operacao   '+
             'where                    '+
             'Cod_AtribH=:k_Cod_AtribH ');
 end;
 tq.parambyname('k_Cod_AtribH').asstring:= Cod_AtribH;
 tq.parambyname('k_Descricao').asstring := Descricao;
 tq.parambyname('k_Percentual').asfloat := Percentual;
 tq.parambyname('k_Operacao').asstring  := Operacao;
 tq.execsql;
 tq.close;
 tq.Free;
end;

procedure TAtrib_Homem.Excluir;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Atrib_Homem where Cod_AtribH=:a');
 tq.params[0].asstring:= Cod_AtribH;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TAtrib_Homem.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Homem where Cod_AtribH=:a');
 tq.params[0].asstring:= Cod_AtribH;
 tq.open;
 if tq.eof then result:= false else result:= true;
 Descricao := tq.fieldbyname('Descricao').asstring;
 Percentual:= tq.fieldbyname('Percentual').asfloat;
 Operacao  := tq.fieldbyname('Operacao').asstring;
 tq.close;
 tq.free;
end;

function TAtrib_Homem.Retorna_Ultimo;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select max(Cod_AtribH) ultimo from Atrib_Homem');
 tq.open;
 if tq.fieldbyname('ultimo').asstring='' then
  result:= '001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,3);
 tq.close;
 tq.Free;
end;

procedure TAtrib_Homem.Consultar;
begin
 tq.close;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Homem where Descricao>=:a order by Descricao');
 if p_Descricao='' then p_Descricao:= ' ';
 tq.params[0].asstring:= p_Descricao;
 tq.open;
end;

end.
